//
//  HomeTableViewController.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/8.
//  Copyright (c) 2015年 name. All rights reserved.
//

import SVProgressHUD
import UIKit
import MJRefresh


class HomeTableViewController: VisitorViewController {

    private var StatusesData = [statusModel]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        if !UserAccountOAuthModel.shared.userlogin {
            
            Viesitor.setImageAndTipText(nil, tip: "关注一些人,回这里看看有什么惊喜")
        
            return
        }
            self.tableView.separatorStyle = .None
            self.tableView.estimatedRowHeight = 200
            self.tableView.rowHeight = UITableViewAutomaticDimension
            self.tableView.registerClass(StatusCell.self, forCellReuseIdentifier: "StatusCell")
        
            self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { () -> Void in
                StatusesViewModel().loadStatuses(since_id: (self.StatusesData.first?.id)!, finished: { (modelData) -> () in
                    if modelData == nil {
                        SVProgressHUD.showErrorWithStatus(MessageTipError)
                        return
                    }
                    
                    self.StatusesData = modelData! + self.StatusesData
                    
                    self.tableView.reloadData()
                    self.tableView.mj_header.endRefreshing()
                })
            })
        
        
        
            self.tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: { () -> Void in
                StatusesViewModel().loadStatuses(max_id: (self.StatusesData.last?.id)!, finished: { (modelData) -> () in
                    if modelData == nil {
                        SVProgressHUD.showErrorWithStatus(MessageTipError)
                        return
                    }
                    self.StatusesData += modelData!
                    self.tableView.reloadData()
                    self.tableView.mj_footer.endRefreshing()
                })
            })
        
        
        loadStatuses()
        
    }
    
    
    
    private func loadStatuses() {
        
    
            StatusesViewModel().loadStatuses(since_id: 0, max_id: 0) { (modelData) -> () in
                
                if modelData?.count < 1 {
                    SVProgressHUD.showInfoWithStatus(MessageTipError)
                    return
                }
            
            self.StatusesData = modelData!
            self.tableView.reloadData()
        }
    
    }
 

}


extension HomeTableViewController  {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return StatusesData.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
      let cell = tableView.dequeueReusableCellWithIdentifier("StatusCell", forIndexPath: indexPath) as! StatusCell
    
     let md = StatusesData[indexPath.row]
        
        
        cell.status = md
        
        return cell
   }
    
    
    
}